package com.xc.common.tool;

import java.util.List;

public class CheckRepeat {
    public static int getEditDistance(String str1,String str2){
        int len1 = str1.length();
        int len2 = str2.length();
        int[][] dp = new int[len1 + 1][len2 + 1];
        for(int i=0;i<=len1;i++){
            dp[i][0] = i;
        }
        for(int i=0;i<=len2;i++){
            dp[0][i] = i;
        }
        for(int i=1;i<=len1;i++){
            for(int j=1;j<=len2;j++){
                if(str1.charAt(i-1)==str2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1];
                }else {
                    dp[i][j] = Math.min(dp[i-1][j-1] + 1,Math.min(dp[i][j-1] + 1,dp[i-1][j] + 1));
                }
            }

        }
        return dp[len1][len2];
    }

    public static boolean checkTheDuplicate(String text, String text2 , double threshold) {
        int distance = getEditDistance(text,text2);
        double similarity = 1 - (double)distance / Math.max(text.length(),text2.length());
        if(similarity>=threshold){
            return true;
        }
        return false;
    }
    public static boolean checkDuplicate(String text, List<String> texts , double threshold) {
        for(String t : texts){
            if(checkTheDuplicate(text,t,threshold)){
                return true;
            }
        }
        return false;
    }
}
